﻿<div id="createDialog" class="crudDialog">
    <form id="createForm" method="post">
        <div class="radio">
            <div class="radio radio-inline radio-success">
                <input id="type_1" type="radio" name="isParent" value="1" checked>
                <label for="type_1">目录 </label>
            </div>
            <div class="radio radio-inline radio-info">
                <input id="type_2" type="radio" name="isParent" value="2">
                <label for="type_2">菜单 </label>
            </div>
            <div class="radio radio-inline radio-warning">
                <input id="type_3" type="radio" name="isParent" value="3">
                <label for="type_3">按钮 </label>
            </div>
        </div>
        <div class="form-group">
            <span class="type2 type3" hidden>
				<select id="pid" name="parentId">
					<option value="0">请选择上级菜单</option>
				</select>
			</span>
        </div>

        <div class="form-group">
            <label for="name">名称</label>
            <input id="name" type="text" class="form-control" name="permissionName" maxlength="20">
        </div>
        <div class="form-group type2 type3" hidden>
            <label for="permissionValue">权限值</label>
            <input id="permissionValue" type="text" class="form-control" name="permissionValue" maxlength="50">
        </div>
        <div class="form-group type2 type3" hidden>
            <label for="url">路径</label>
            <input id="url" type="text" class="form-control" name="permissionUrl" maxlength="100">
        </div>
        <div class="form-group text-right dialog-buttons">
            <a class="waves-effect waves-button" href="javascript:;" onclick="createSubmit();">保存</a>
            <a class="waves-effect waves-button" href="javascript:;" onclick="createDialog.close();">取消</a>
        </div>
    </form>
</div>
<script>
    var pidType = 0;
    var systemId = 0;
    var type = 1;
    $(function () {
        // 选择分类
        $('input:radio[name="isParent"]').change(function () {
            type = $(this).val();
            initType();
        });
    });

    function initType() {
        // 显示对应必填项
        $('.type1,.type2,.type3').hide(0, function () {
            $('.type' + type).show();
        });
        // 级联菜单
        if (type == 2) {
            pidType = 1;
            initPid();
        }
        if (type == 3) {
            pidType = 2;
            initPid();
        }
    }

    function initPid() {
        $.getJSON('parentlist', function (json) {
            var datas = [{id: 0, text: '请选择上级'}];
            for (var i = 0; i < json.rows.length; i++) {
                var data = {};
                data.id = json.rows[i].id;
                data.text = json.rows[i].permissionName;
                datas.push(data);
            }
            $('#pid').empty();
            $('#pid').select2({
                data: datas
            });
        });
    }

    function createSubmit() {
        $.ajax({
            type: 'post',
            url: 'create',
            data: $('#createForm').serialize(),
            beforeSend: function () {
                if (type == 1) {
                    if ($('#name').val() == '') {
                        $('#name').focus();
                        return false;
                    }
                }
                if (type == 2 || type == 3) {
                    if ($('#pid').val() == 0) {
                        $.confirm({
                            title: false,
                            content: '请选择上级！',
                            autoClose: 'cancel|3000',
                            backgroundDismiss: true,
                            buttons: {
                                cancel: {
                                    text: '取消',
                                    btnClass: 'waves-effect waves-button'
                                }
                            }
                        });
                        return false;
                    }
                    if ($('#name').val() == '') {
                        $('#name').focus();
                        return false;
                    }
                    if ($('#permissionValue').val() == '') {
                        $('#permissionValue').focus();
                        return false;
                    }
                    if ($('#url').val() == '') {
                        $('#url').focus();
                        return false;
                    }
                }
            },
            success: function (result) {
                if (result.code != 0) {
                    $.confirm({
                        theme: 'dark',
                        animation: 'rotateX',
                        closeAnimation: 'rotateX',
                        title: false,
                        content: result.errMsg,
                        buttons: {
                            confirm: {
                                text: '确认',
                                btnClass: 'waves-effect waves-button waves-light'
                            }
                        }
                    });
                } else {
                    createDialog.close();
                    $table.bootstrapTable('refresh');
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $.confirm({
                    theme: 'dark',
                    animation: 'rotateX',
                    closeAnimation: 'rotateX',
                    title: false,
                    content: textStatus,
                    buttons: {
                        confirm: {
                            text: '确认',
                            btnClass: 'waves-effect waves-button waves-light'
                        }
                    }
                });
            }
        });
    }
</script>